package threadLock.threadBasics.lock;

/**
 * 模拟一个死锁
 */
public class deadlockDemo {
    private static Object resourceA = new Object();
    private static Object resourceB = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(() -> {
            synchronized (resourceA){
                System.out.println(Thread.currentThread().getName() + " get resourceA");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " waiting get resourceB");
                synchronized (resourceB){
                    System.out.println(Thread.currentThread().getName() + " get resourceB");
                }
            }
        },"threadA");

        Thread threadB = new Thread(() -> {
            synchronized (resourceB){
                System.out.println(Thread.currentThread().getName() + " get resourceB");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " waiting get resourceA");
                synchronized (resourceA){
                    System.out.println(Thread.currentThread().getName() + " get resourceA");
                }
            }
        },"threadB");
        threadA.start();
        threadB.start();
    }
}
